<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 2024/2/9
  Time: 19:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
文档列表:
<select name="categoryId">
    <option value="">全部</option>
</select>
<button onclick="query()" id="query">查询</button>
<button onclick="add()" id="add">新增电子文档</button>
<h1>电子文档列表</h1>
<table border="1">
    <tr>
        <td>文档编号</td>
        <td>文档名称</td>
        <td>文档摘要</td>
        <td>上传人</td>
        <td>上传时间</td>
        <td>操作</td>
    </tr>
    <c:forEach items="${list}" var="list">
        <tr>
            <td>${list.id}</td>
            <td>${list.title}</td>
            <td>${list.summary}</td>
            <td>${list.uploaduser}</td>
            <td><fmt:formatDate value="${list.createdate}" pattern="yyyy-MM-dd" /></td>
            <td>
                <a href="javascript:void(0)" onclick="update(${list.id})">修改</a>
                <a href="javascript:void(0)" onclick="del(${list.id})">删除</a>
            </td>
        </tr>
    </c:forEach>
</table>
<p>
    <a href="${pageContext.request.contextPath}/list?startIndex=1">首页</a>
    <a href="${pageContext.request.contextPath}/list?startIndex=${startIndex-1}">上一页</a>
    <a href="${pageContext.request.contextPath}/list?startIndex=${startIndex+1}">下一页</a>
    <a href="${pageContext.request.contextPath}/list?startIndex=${pageCount}">尾页</a>
    <span>第${startIndex}页/共${pageCount}页</span>
</p>
</body>
<script src="${pageContext.request.contextPath}/static/js/jquery.min.js"></script>
<script>
    var contextPath = "<%= request.getContextPath() %>";
    $(function () {
        category();
    })

    function category() {

        $.get('${pageContext.request.contextPath}/categoryList', function (data) {
            console.log(data)
            let list = data;
            let selectElement = $("select[name='categoryId']"); // 获取选择框元素
            let categoryId = parseInt('${categoryId}');
            for (let i = 0; i < list.length; i++) {
                // 创建选项并添加到选择框中
                selectElement.append(`<option <c:if test="\${categoryId==list[i].id}">selected</c:if> value="\${list[i].id}">\${list[i].name}</option>`);
            }
        },'json');
    }

    function query() {
        let categoryId = $("select[name='categoryId']").val();
        location.href=contextPath+"/list?categoryId=" + categoryId
    }

    function add(){
        location.href=contextPath+'/add';
    }

    function update(id){
        location.href='selectById?id='+id;
    }

    function del(id){
        if(confirm("确定要删除吗？")){
            location.href='delete?id='+id;
        }
    }
</script>
</html>
